package com.tianqu.tms.www.transfer.controller;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.security.TmsUserProfile;
import com.tianqu.tms.entity.TransferAcceptedInfo;
import com.tianqu.tms.entity.TransferProcessHistory;
import com.tianqu.tms.www.transfer.actionform.CheckConfirmActionForm;

public class IndustryConfirmInputResultController extends TmsController {
	private Logger logger = Logger.getLogger(IndustryConfirmInputResultController.class);

	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = getTxController();
		try {
			controller.begin();

			String serial = (String) uc.getAttribute("serial");

			CheckConfirmActionForm form = (CheckConfirmActionForm) uc
					.getInputForm();

			TransferAcceptedInfo accept = new TransferAcceptedInfo();
			TransferProcessHistory his = new TransferProcessHistory();
			TmsUserProfile profile = uc.getUserProfile();
			accept = accept.getAcceptInfoBySerial(serial, "");
			if (form.getTgBsFlag().equals("1")) {
				// 更新过户信息表
				accept.setSerial(serial);
				accept.setStatus(7);
				accept.setUpdateUserId(profile.getUniqueId());
				TransferAcceptedInfo accept1 = new TransferAcceptedInfo();
				accept1 = accept1.getAcceptInfoBySerial(serial,"");
				accept.setChangeTradeTicket(accept1.getChangeTradeTicket());
				accept.updateAcceptStatusBySerial(accept);
				// 更新历史表
				his.setSerial(serial);
				his.setBusinessType(7);
				his.setInsertUserId(profile.getUniqueId());
				his.setReason("");
				his.insertHisInfoAtAssess(his);
			}
			if (form.getTgBsFlag().equals("2")) {
				his.setSerial(serial);
				his.setBusinessType(32);
				his.setInsertUserId(profile.getUniqueId());
				his.setReason(form.getReason());
				his.insertHisInfoAtAssess(his);
			}

			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}
		logger.debug(this.getClass().getName() + "#execute was completed.");
	}
}
